<?php
	require 'config.php';
	require 'function.php';
	
	if($success = TRUE)
		echo "SUCCESS";
	
	$usernamenotempty = TRUE;
	$usernamevalidate = TRUE;
	$usernamenotduplicate = TRUE;
	$passwordnotempty = TRUE;
	$passwordmatch = TRUE;
	$passwordvalidate = TRUE;
	$captchavalidation = TRUE;
	$success = FALSE;
	
	if((isset($_POST['desired_password'])) && (isset($_POST['desired_username'])) && (isset($_POST['desired_password1'])))
	{
		
		$desired_username = santize($_POST['desired_username']);
		$desired_password = santize($_POST['desired_password']);
		$desifre_password1 = santize($_POST['desired_password1']);
		
		if(empty($desired_username))
		{
			$usernamenotempty = FALSE;
		} else
		{
			$usernamenotempty = TRUE;
		}
		
		if((!(ctype_alnum($desired_username))) || ((strlen($desired_username)) > 11))
		{
			$usernamevalidate = FALSE;
		} else
		{
			$usernamevalidate = TRUE;
		}				
		
		if (!($fetch = mysql_fetch_array(mysql_query("SELECT username FROM users WHERE username ='$desired_username'"))))
		{
			$usernamenotduplicate = TRUE;
		} else
		{
			$usernamenotduplicate = FALSE;
		}
		
		if(empty($desired_password)) 
		{
			$passwordnotempty = FALSE;
		} else
		{
			$passwordnotempty = TRUE;
		}
		
		if((!(ctype_alnum($desired_password))) || (strlen($desired_password) < 8))
		{
			$passwordvalidate = FALSE;
		} else
		{
			$passwordvalidate = TRUE;
		}
		
		if($desired_password == $desifre_password1)
		{
			$passwordmatch = TRUE;			
		} else
		{
			$passwordmatch = FALSE;
		}
		/*
		//Validate recaptcha
		require_once('recaptchalib.php');
		$resp = recaptcha_check_answer($privatekey, $_SERVER["REMOTE_ADDR"],
				$_POST["recaptcha_challenge_field"], $_POST["recaptcha_response_field"]);
		
		if (!$resp->is_valid) {
			//captcha validation fails
			$captchavalidation = FALSE;
		} else {
			$captchavalidation = TRUE;
		}
		*/
	
		/*
		echo "1" . $usernamevalidate . "<br/>";
		echo "2" . $usernamenotempty . "<br/>";
		echo "3" . $usernamenotduplicate . "<br/>";
		echo "4" . $passwordnotempty . "<br/>";
		echo "5" . $passwordvalidate . "<br/>";
		echo "6" . $passwordmatch . "<br/>";
		*/
		
		if(($usernamenotempty == TRUE)
			&& ($usernamevalidate == TRUE)
			&& ($usernamenotduplicate == TRUE)
			&& ($passwordnotempty == TRUE)
			&& ($passwordvalidate == TRUE)
			&& ($passwordmatch == TRUE))
		{		
			
			$salt = genSalt();			
			$hashedpassword = genHashPassword($desired_password, $salt);
			
			mysql_query("INSERT INTO users (username,password,salt) VALUES ('$desired_username','$hashedpassword','$salt')") or die(mysql_error());
			
			$message = "New member has just registered : $desired_username";
			//$message .= "$desired_password";
			$success = TRUE;
			
			//mail($email, $subject, $message, $from);
			//header(sprintf("Loaction : %s", $logginpage_url));
			//headr(sprintf("Location: register.php"));
		}		
	}
?>
<!DOCTYPE HTML>
<html>
    <head>
        <title>Register as a Valid User</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <style type="text/css">
            .invalid {
                border: 1px solid #000000;
                background: #FF00FF;
            }
        </style>
    </head>
    <body >    
        <!-- Start of registration form -->
        <form action="register.php" method="POST">
        Username : <input type="text" id="desired_username" name=desired_username /> <br/>
		Password : <input type="password" id="desired_password" name="desired_password" /> <br/>
		Re-type Password : <input type="password" id="desired_password1" name="desired_password1" /> <br/>
		<input type="submit" value="submit"/>		                
        </form>
        <!-- End of registration form -->
    </body>
</html>